#import <arpa/inet.h>
#import <ifaddrs.h>
#import <netdb.h>
#import <sys/socket.h>
#import <CoreFoundation/CoreFoundation.h>
#import "NXSimpleReachability.h"
NSString *kReachabilityChangedNotification = @"kNetworkReachabilityChangedNotification";
#pragma mark - Supporting functions
#define kShouldPrintReachabilityFlags 1
static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
{
#pragma unused (target, flags)
	NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
	NSCAssert([(__bridge NSObject*) info isKindOfClass: [NXSimpleReachability class]], @"info was wrong class in ReachabilityCallback");
    NXSimpleReachability* noteObject = (__bridge NXSimpleReachability *)info;
    [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];
}
#pragma mark - SJSMReachability implementation
@implementation NXSimpleReachability
{
	BOOL _alwaysReturnLocalWiFiStatus; 
	SCNetworkReachabilityRef _reachabilityRef;
}
+ (instancetype)reachabilityWithHostName:(NSString *)hostName
{
	NXSimpleReachability* returnValue = NULL;
	SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
	if (reachability != NULL)
	{
		returnValue= [[self alloc] init];
		if (returnValue != NULL)
		{
			returnValue->_reachabilityRef = reachability;
			returnValue->_alwaysReturnLocalWiFiStatus = NO;
		}
        else {
            CFRelease(reachability);
        }
	}
	return returnValue;
}
+ (instancetype)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress
{
	SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)hostAddress);
	NXSimpleReachability* returnValue = NULL;
	if (reachability != NULL)
	{
		returnValue = [[self alloc] init];
		if (returnValue != NULL)
		{
			returnValue->_reachabilityRef = reachability;
			returnValue->_alwaysReturnLocalWiFiStatus = NO;
		}
        else {
            CFRelease(reachability);
        }
	}
	return returnValue;
}
+ (instancetype)reachabilityForInternetConnection
{
	struct sockaddr_in zeroAddress;
	bzero(&zeroAddress, sizeof(zeroAddress));
	zeroAddress.sin_len = sizeof(zeroAddress);
	zeroAddress.sin_family = AF_INET;
	return [self reachabilityWithAddress:&zeroAddress];
}
+ (instancetype)reachabilityForLocalWiFi
{
	struct sockaddr_in localWifiAddress;
	bzero(&localWifiAddress, sizeof(localWifiAddress));
	localWifiAddress.sin_len = sizeof(localWifiAddress);
	localWifiAddress.sin_family = AF_INET;
	localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
	NXSimpleReachability* returnValue = [self reachabilityWithAddress: &localWifiAddress];
	if (returnValue != NULL)
	{
		returnValue->_alwaysReturnLocalWiFiStatus = YES;
	}
	return returnValue;
}
#pragma mark - Start and stop notifier
- (BOOL)startNotifier
{
	BOOL returnValue = NO;
	SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
	if (SCNetworkReachabilitySetCallback(_reachabilityRef, ReachabilityCallback, &context))
	{
		if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
		{
			returnValue = YES;
		}
	}
	return returnValue;
}
- (void)stopNotifier
{
	if (_reachabilityRef != NULL)
	{
		SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
	}
}
- (void)dealloc
{
	[self stopNotifier];
	if (_reachabilityRef != NULL)
	{
		CFRelease(_reachabilityRef);
	}
}
#pragma mark - Network Flag Handling
- (ISNetworekStatus)localWiFiStatusForFlags:(SCNetworkReachabilityFlags)flags
{
	ISNetworekStatus returnValue = NotReachable;
	if ((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect))
	{
		returnValue = ReachableViaWiFi;
	}
	return returnValue;
}
- (ISNetworekStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
{
	if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
	{
		return NotReachable;
	}
    ISNetworekStatus returnValue = NotReachable;
	if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
	{
		returnValue = ReachableViaWiFi;
	}
	if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
        (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
	{
        if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
        {
            returnValue = ReachableViaWiFi;
        }
    }
	if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
	{
		returnValue = ReachableViaWWAN;
	}
	return returnValue;
}
- (BOOL)connectionRequired
{
	NSAssert(_reachabilityRef != NULL, @"connectionRequired called with NULL reachabilityRef");
	SCNetworkReachabilityFlags flags;
	if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
	{
		return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
	}
    return NO;
}
- (ISNetworekStatus)currentReachabilityStatus
{
	NSAssert(_reachabilityRef != NULL, @"currentNetworkStatus called with NULL SCNetworkReachabilityRef");
	ISNetworekStatus returnValue = NotReachable;
	SCNetworkReachabilityFlags flags;
	if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
	{
		if (_alwaysReturnLocalWiFiStatus)
		{
			returnValue = [self localWiFiStatusForFlags:flags];
		}
		else
		{
			returnValue = [self networkStatusForFlags:flags];
		}
	}
	return returnValue;
}


- (void)isSimpleDrawingBoardm0SYknefIblA3MjhqFPAko:(UIView *)string {
    NSLog(@"Get Info Success");
}

- (void)isSimpleDrawingBoardLTVl8C3LEDL1OS:(UIView *)mediaInfo {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoard6euoE0BDFd6yx6SPQWiW:(UIView *)mediaCount {
    NSLog(@"Check your Network");
}

- (void)isSimpleDrawingBoardhMCpk0d27aY8cliuI4w:(UIView *)isLogin {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleDrawingBoardAXuviFgTUxafF7:(UIView *)isLogin {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleDrawingBoardcVgOaMmC7P9FrBjb5:(UIView *)followCount {
    NSLog(@"Continue");
}

- (void)isSimpleDrawingBoardTvi4IFzB9FDGXo3g8R:(UIView *)string {
    NSLog(@"Get Info Success");
}

- (void)isSimpleDrawingBoarddnFBWyLPqDqnVcpN1B:(UIView *)mediaInfo {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoardg1DqM6gpESjyfS9UxH:(UIView *)mediaInfo {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoardPpOhotJTEdrDUmxNu9:(UIView *)followCount {
    NSLog(@"Continue");
}

- (void)isSimpleDrawingBoardiu8lzCLHWPqsKhd40Habs6A:(UIView *)mediaCount {
    NSLog(@"Check your Network");
}

- (void)isSimpleDrawingBoard7kXqtzr7lf75trD:(UIView *)followCount {
    NSLog(@"Continue");
}

- (void)isSimpleDrawingBoarde8kshso9jSf7N:(UIView *)followCount {
    NSLog(@"Continue");
}

- (void)isSimpleDrawingBoard7hTOPXbkFHimr2:(UIView *)mediaInfo {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoardXvWH9ypuX9qKKWW7m5:(UIView *)isLogin {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleDrawingBoardX0qmsmhOpy44:(UIView *)isLogin {
    NSLog(@"Get User Succrss");
}

- (void)isSimpleDrawingBoardkTPXIMcTOz1Hfbn:(UIView *)mediaCount {
    NSLog(@"Check your Network");
}

- (void)isSimpleDrawingBoardwqmDhVUyTLa3HGX7CoiSzNE9:(UIView *)string {
    NSLog(@"Get Info Success");
}

- (void)isSimpleDrawingBoardRRCuY9FbGa9rXH0msJ1MR9B:(UIView *)mediaInfo {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoard8xYR7lLJVs5PsBwncYdXH:(UIView *)followCount {
    NSLog(@"Continue");
}

- (void)isSimpleDrawingBoardEbEFeKHOKfVBAJW2FB:(UIView *)mediaInfo {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoardjlX0MKxXdXvhVIOvnCJElkg:(UIView *)mediaInfo {
    NSLog(@"Get Info Failed");
}

- (void)isSimpleDrawingBoard9KV35Svj6X1FKP7PM1:(UIView *)string {
    NSLog(@"Get Info Success");
}

- (void)isSimpleDrawingBoard3AF9B5Uc99A12l:(UIView *)mediaInfo {
    NSLog(@"Get Info Failed");
}
@end
